import { defineStore } from 'pinia';
import { ref } from 'vue';
import { getDict as getDictApi, getUnreadNum } from '@/api';
import { setCache, getCache, removeCache, ArrToObj } from '@/utils';
// import { SOCKET_STATUS } from '@/types';
import { user } from '@/store';

export const global = defineStore('global', () => {
    const dict = ref(getCache('dict') || {});
    // 获取字典列表
    const getDicts = async () => {
        const res = await getDictApi();
        dict.value = res.data;
        setCache('dict', res.data);
    };

    // 获取字典
    const getDict = (key: string, full = false) => {
        const d = dict.value.find((i: anyObj) => i.code === key);
        const child = d ? d.children : [];
        if (full) {
            return {
                dict: child,
                dictObj: ArrToObj(child, 'value', 'code'),
            };
        }
        return child;
    };

    const clear = () => {
        dict.value = {};
        removeCache('dict');
    };

    return {
        dict,
        getDicts,
        getDict,
        clear,
    };
});
